package com.yuke.cloud.service.cmc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.cmc.dto.CmsCouponUserDto;
import com.yuke.cloud.service.api.smc.dto.ShopInfoDto;
import com.yuke.cloud.service.api.smc.service.SmcFeignApi;
import com.yuke.cloud.service.api.uac.dto.UserInfoDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.cmc.dao.CmsCouponReceiveMapper;
import com.yuke.cloud.service.cmc.dao.CmsCouponSpecMapper;
import com.yuke.cloud.service.cmc.entity.CmsCouponReceive;
import com.yuke.cloud.service.cmc.service.CmsCouponReceiveService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by wg on 2019/09/25.
 */
@Service
@Transactional
public class CmsCouponReceiveServiceImpl extends BaseService<CmsCouponReceive> implements CmsCouponReceiveService {
    @Resource
    private CmsCouponReceiveMapper cmsCouponReceiveMapper;
    @Resource
    private SmcFeignApi smcFeignApi;
    @Resource
    private UacUserFeignApi uacUserFeignApi;
    @Resource
    private CmsCouponSpecMapper cmsCouponSpecMapper;

    @Override
    public List<CmsCouponUserDto> getCouponUserList(CmsCouponUserDto cmsCouponUserDto) {
        return cmsCouponReceiveMapper.getCouponUserList(cmsCouponUserDto);
    }


    @Override
    public List<CmsCouponUserDto> getCouponAffiliateList(){
        return cmsCouponReceiveMapper.getCouponAffiliateList();
    }

    @Override
    public boolean modifyCouponStatus(Long id,Long orderId){
        CmsCouponReceive cmsCouponReceive = cmsCouponReceiveMapper.selectByPrimaryKey(id);
        cmsCouponReceive.setCouponStatus((byte)1);
        cmsCouponReceive.setOrderId(orderId);
        Date nowTime = new Date();
        cmsCouponReceive.setUsedTime(nowTime);
        cmsCouponReceive.setUpdateTime(nowTime);
        cmsCouponReceiveMapper.updateByPrimaryKey(cmsCouponReceive);
        return true;
    }

    @Override
    public boolean updateCouponStatus(Long id){
        CmsCouponReceive cmsCouponReceive = cmsCouponReceiveMapper.selectByPrimaryKey(id);
        cmsCouponReceive.setCouponStatus((byte)0);
        Date nowTime = new Date();
        cmsCouponReceive.setUsedTime(nowTime);
        cmsCouponReceive.setUpdateTime(nowTime);
        cmsCouponReceiveMapper.updateByPrimaryKey(cmsCouponReceive);
        return true;
    }

    @Override
    public List<CmsCouponUserDto> getCouponListByReceiveId( Long receiveId, Integer sendAspect){
        CmsCouponUserDto cmsCouponUserDto = new CmsCouponUserDto();
        cmsCouponUserDto.setReceiveId(receiveId);
        cmsCouponUserDto.setCouponStatus((byte)0);
        cmsCouponUserDto.setSendAspect(sendAspect);
        List<CmsCouponUserDto> list = cmsCouponReceiveMapper.getCouponUserList(cmsCouponUserDto);
        return list;
    }

    @Override
    public List<CmsCouponUserDto> getCouponDetails(Long couponId) {
        return cmsCouponReceiveMapper.getCouponDetails(couponId);
    }

    @Override
    public List<CmsCouponUserDto> getCouponUserListByCondition(CmsCouponUserDto cmsCouponUserDto) {
        return cmsCouponReceiveMapper.getCouponUserListByCondition(cmsCouponUserDto);
    }

    @Override
    public List<CmsCouponUserDto> addShopInfo(List<CmsCouponUserDto> cmsCouponUserDtos) {
        List<Long> shopIds = new ArrayList<>();
        List<Long> userIds = new ArrayList<>();
        for (CmsCouponUserDto item : cmsCouponUserDtos){
            if (item.getReceiveType().equals((byte)1)){
                shopIds.add(item.getReceiveId());
            }
            else if (item.getReceiveType().equals((byte)0)){
                userIds.add(item.getReceiveId());
            }
        }

        List<ShopInfoDto> shopInfoDtoList = new ArrayList<>();
        if (shopIds != null && shopIds.size() > 0){
            shopInfoDtoList = smcFeignApi.getShopUserIdByShopIds(shopIds);
        }

        List<UserInfoDto> userInfoDtoList = new ArrayList<>();
        if (userIds != null && userIds.size() > 0){
            Wrapper<List<UserInfoDto>> wrapper = uacUserFeignApi.getUserInfoByUserIds(shopIds);
            if (wrapper != null && wrapper.getCode() == 200){
                userInfoDtoList = wrapper.getResult();
            }
        }

        for (CmsCouponUserDto dto : cmsCouponUserDtos){
            if (dto.getReceiveType().equals((byte)1) && shopInfoDtoList.size() > 0){
                for (ShopInfoDto shop : shopInfoDtoList){
                    if (dto.getReceiveId().equals(shop.getShopId())){
                        dto.setShopName(shop.getShopName());
                    }
                }
            }else if (dto.getReceiveType().equals((byte)0) && userInfoDtoList.size() > 0){
                for (UserInfoDto user : userInfoDtoList){
                    if (dto.getReceiveId().equals(user.getUserId())){
                        dto.setUserName(user.getUserName());
                    }
                }
            }

        }

        return cmsCouponUserDtos;
    }
}
